package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IAnswerCacheService;
import cn.wolfcode.luowowo.cache.service.IQuestionCacheService;
import cn.wolfcode.luowowo.cache.vo.AnswerStats;
import cn.wolfcode.luowowo.cache.vo.QuestionStats;
import cn.wolfcode.luowowo.comment.domain.AnswerComment;
import cn.wolfcode.luowowo.comment.domain.QuestionComment;
import cn.wolfcode.luowowo.comment.service.IAnswerCommentService;
import cn.wolfcode.luowowo.comment.service.IQuestionCommentService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class QuestionStatsDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IQuestionCommentService questionCommentService;
    @Reference
    private IQuestionCacheService questionCacheService;
    @Reference
    private IAnswerCommentService answerCommentService;
    @Reference
    private IAnswerCacheService answerCacheService;

    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //完成数据的统计,将mongoDB中的数据导入redis
        //1.查询所有的提问
        List<QuestionComment> QuestionComments = questionCommentService.findAll();

        //2.迭代list对象
        for (QuestionComment questionComment : QuestionComments) {
            //判断vo对象在redis中是否已经存在,如过不存在就存入redis中
            if (!questionCacheService.hasKey(questionComment.getId())) {
                //3封装成vo对象
                QuestionStats vo = new QuestionStats();
                vo.setQId(questionComment.getId());
                vo.setReplynum(questionComment.getReplynum());
                vo.setViewnum(questionComment.getViewnum());

                //分享数没有做,把分享数当做是关注数来做
                vo.setFavornum(questionComment.getSharenum());
                vo.setThumbsupnum(questionComment.getThumbsupnum());
                //保存到redis中
                questionCacheService.setQuestionStats(vo);

            }
            List<AnswerComment> answerComments = answerCommentService.findById(questionComment.getId());
            for (AnswerComment answerComment : answerComments) {
                if (!answerCacheService.hasKey(answerComment.getId())) {
                    AnswerStats vo = new AnswerStats();
                    vo.setAId(answerComment.getId());
                    vo.setUserId(answerComment.getUserId());
                    vo.setQId(questionComment.getId());
                    vo.setHeadUrl(answerComment.getHeadUrl());
                    vo.setLevel(answerComment.getLevel());
                    vo.setUsername(answerComment.getUsername());
                    vo.setThumbsupnum(answerComment.getThumbupnum());
                    //保存到redis中
                    answerCacheService.setAnswerStats(vo);
                }

            }
        }
    }
}
